package com.zhxh.nzpos.goods;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.zhxh.nzpos.BaseActivity;
import com.zhxh.nzpos.R;
import com.zhxh.nzpos.dao.CategoryDao;
import com.zhxh.nzpos.dao.GoodsDao;
import com.zhxh.nzpos.model.Category;
import com.zhxh.nzpos.model.Goods;
import com.zhxh.nzpos.utils.ViewUtil;

/**
 * 商品添加
 * 
 * @author wangzt
 * 
 */
public class GoodsAddActivity extends BaseActivity {

	private String[] category_items;
	List<Category> list;
	private String goodsid;
	private Category cate;
	private TextView cate1;

	EditText identifier;
	EditText name;
	EditText unit1;
	EditText price1;
	EditText unit2;
	EditText price2;
	EditText specification;
	EditText code;
	Spinner category;
	GoodsDao dao;
	CategoryDao cateDao;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_goods_add);
		dao = new GoodsDao(getApplicationContext());
		cateDao = new CategoryDao(this);
		initViews();

	}

	private void initViews() {

		identifier = (EditText) findViewById(R.id.identifier);
		name = (EditText) findViewById(R.id.name);
		// EditText category = (EditText) findViewById(R.id.category);
		unit1 = (EditText) findViewById(R.id.unit1);
		price1 = (EditText) findViewById(R.id.price1);
		
		
		unit2 = (EditText) findViewById(R.id.unit2);
		price2 = (EditText) findViewById(R.id.price2);
		specification = (EditText) findViewById(R.id.specification);
		// EditText points = (EditText) findViewById(R.id.points);
		code = (EditText) findViewById(R.id.code);

		category = (Spinner) findViewById(R.id.spinner1);
		cate1 = (TextView) findViewById(R.id.cate1);

		goodsid = getIntent().getStringExtra("goodsid");

		Bundle b = getIntent().getExtras();
		cate = (Category) b.getSerializable("cate");
		cate1.setText(cate.getCategory() + ":");

		list = cateDao.getCategorysById(cate.getId());
		category_items = new String[list.size() + 1];
		category_items[0] = "请选择分类";
		for (int i = 0; i < list.size(); i++) {
			Category cate = list.get(i);
			category_items[i + 1] = cate.getCategory();
		}
		
		price1.addTextChangedListener(new TextWatcher()
		{
			public void afterTextChanged(Editable edt)
			{
				String temp = edt.toString();
				int posDot = temp.indexOf(".");
				if (posDot <= 0)
					return;
				if (temp.length() - posDot - 1 > 2)
				{
					edt.delete(posDot + 3, posDot + 4);
				}
			}
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
			}
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
			}

		});
		
		price2.addTextChangedListener(new TextWatcher()
		{
			public void afterTextChanged(Editable edt)
			{
				String temp = edt.toString();
				int posDot = temp.indexOf(".");
				if (posDot <= 0)
					return;
				if (temp.length() - posDot - 1 > 2)
				{
					edt.delete(posDot + 3, posDot + 4);
				}
			}
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
			}
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
			}

		});
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, category_items);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// adapter.setDropDownViewResource(R.layout.myspinnerdropdown);

		category.setAdapter(adapter);

		if (goodsid != null && goodsid.length() != 0) {
			Goods goods = dao.find(goodsid);
			identifier.setText(goods.getId());
			identifier.setEnabled(false);
			name.setText(goods.getName());
			unit1.setText(goods.getUnits1());
			price1.setText(goods.getPrice1() + "");
			unit2.setText(goods.getUnits2());
			price2.setText(goods.getPrice2() + "");
			specification.setText(goods.getSpecification());
			code.setText(goods.getCode());
			for (int i = 0; i < category_items.length; i++) {
				String cateName = category_items[i];
				if (cateName.equals(goods.getCategoryName())) {
					category.setSelection(i);
					break;
				}
			}

		}

	}

	/**
	 * 添加商品
	 * @param view
	 */
	public void add(View view) {

		Goods good =  dao.find(identifier.getText().toString());
		if(good != null) {
			ViewUtil.makeToast(this, "此编号的商品已经存在！请重新输入");
			return;
		}
		// EditText note = (EditText) findViewById(R.id.note);
		boolean isOK = true;
		int position = category.getSelectedItemPosition();
		if (identifier.getText().toString().length() == 0) {
			identifier.setError("商品编码不能为空");
			isOK = false;
		}
		if (name.getText().toString().length() == 0) {
			name.setError("商品名称不能为空");
			isOK = false;
		}

		if (unit1.getText().toString().length() == 0) {
			unit1.setError("商品单位不能为空");
			isOK = false;
		}
		if (price1.getText().toString().length() == 0) {
			price1.setError("商品价格不能为空");
			isOK = false;
		}
		if (specification.getText().toString().length() == 0) {
			specification.setError("规格不能为空");
			isOK = false;
		}
		if (code.getText().toString().length() == 0) {
			code.setError("条码不能为空");
			isOK = false;
		}

		if (position == 0) {
			ViewUtil.makeToast(this, "请选择商品分类");
			isOK = false;
		}
		if (!isOK)
			return;

		Category cate = list.get(position - 1);

		Goods goods = new Goods();
		goods.setId(identifier.getText().toString());
		goods.setCategoryId(cate.getId());
		goods.setCategoryName(cate.getCategory());
		goods.setCode(code.getText().toString());
		goods.setName(name.getText().toString());
		goods.setUnits1(unit1.getText().toString());
		goods.setUnits2(unit2.getText().toString());
		goods.setPrice1(Float.valueOf(price1.getText().toString()));
		goods.setPrice2(Float.valueOf(price2.getText().toString()));
		goods.setSpecification(specification.getText().toString());
		goods.setPonits(0);
		goods.setNote("");
		goods.setType(1);
		
		if (goodsid != null && goodsid.length() != 0) {
			dao.update(goods);
			ViewUtil.makeToast(this, "修改商品信息成功");
		} else {
			Goods gd = dao.find(identifier.getText().toString());
			if(gd != null) {
				ViewUtil.makeToast(this, "该编号商品已经存在");
				return;
			}
			dao.save(goods);
			ViewUtil.makeToast(this, "添加商品信息成功");
		}

		

		GoodsItem item = new GoodsItem();
		item.setId(goods.getId());
		item.setCategoryId(goods.getCategoryId());
		item.setCategoryName(goods.getCategoryName());
		item.setName(goods.getName());
		item.setNote(goods.getNote());
		item.setPrice1(goods.getPrice1());
		item.setPrice2(goods.getPrice2());
		item.setSpecification(goods.getSpecification());
		item.setUnits1(goods.getUnits1());
		item.setUnits2(goods.getUnits2());
		item.setCode(goods.getCode());
		item.setPoints(goods.getPonits());

		Intent i = new Intent();
		i.putExtra("goodsid", goodsid);
		Bundle b = new Bundle();
		b.putSerializable("goods", item);
		i.putExtras(b);

		setResult(RESULT_OK, i);
		finish();

	}

	public void cancel(View view) {

		finish();

	}
}
